//
//  XDBaseViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "XDBaseViewController.h"
#import "XDHeader.h"

#define LEFTBUTTONFRAME(aHeight) CGRectMake(0,0+aHeight,60,44)


@interface XDBaseViewController ()

@end

@implementation XDBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden =YES;
    aHeight = 0;
    if (IOS7){
        SETSTATUSBARTEXTBLACKCOLORE(YES);
        aHeight =UI_STATUS_BAR_HEIGHT;
        self.navigationBarView = [[UIView alloc] initWithFrame:CGRectMake(0,0,UI_SCREEN_WIDTH, UI_NAVIGATION_BAR_HEIGHT+UI_STATUS_BAR_HEIGHT)];
        _navigationBarBg = [[UIImageView alloc] init];
        _navigationBarBg.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIGATION_BAR_HEIGHT+UI_STATUS_BAR_HEIGHT);
        _navigationBarBg.backgroundColor = UIColorFromRGB(0xf1f1f1);
//        _navigationBarBg.image = [UIImage imageNamed:NAVIGATIONBGIMAGEIOS7];
        [_navigationBarView addSubview:_navigationBarBg];
        [self.view addSubview:_navigationBarView];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, UI_STATUS_BAR_HEIGHT, UI_SCREEN_WIDTH-100, UI_NAVIGATION_BAR_HEIGHT)];
        _titleLabel.textColor = NAVIGATIONTITLECOLORE;
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
        [_navigationBarBg addSubview:_titleLabel];
        
        UIView * l = [UIView initViewRect:CGRectMake(0,_navigationBarView.frame.size.height-0.5,UI_SCREEN_WIDTH, 0.5) andBackGroudColor:UIColorFromRGB(0xcac9c8)];
        [_navigationBarBg addSubview:l];
    }else{
        self.navigationBarView = [[UIView alloc] initWithFrame:CGRectMake(0,0,UI_SCREEN_WIDTH, UI_NAVIGATION_BAR_HEIGHT)];
        _navigationBarBg = [[UIImageView alloc] init];
        _navigationBarBg.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIGATION_BAR_HEIGHT);
        _navigationBarBg.backgroundColor = UIColorFromRGB(0xf1f1f1);
//        _navigationBarBg.image = [UIImage imageNamed:NAVIGATIONBGIMAGE];
        [_navigationBarView addSubview:_navigationBarBg];
        [self.view addSubview:_navigationBarView];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,0, UI_SCREEN_WIDTH-100, UI_NAVIGATION_BAR_HEIGHT)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = NAVIGATIONTITLECOLORE;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = NAVIGATIONTITLEFONT;
        [_navigationBarBg addSubview:_titleLabel];
        
        UIView * l = [UIView initViewRect:CGRectMake(0,_navigationBarView.frame.size.height-0.5,UI_SCREEN_WIDTH, 0.5) andBackGroudColor:UIColorFromRGB(0xcac9c8)];
        [_navigationBarBg addSubview:l];
    }
    
   [self createLeftBtn];
    
    self.contentView= [[UIView alloc] initWithFrame:CGRectMake(0, height_y(_navigationBarView), UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_NAVIGATION_BAR_HEIGHT)];
    _contentView.backgroundColor = UIColorFromRGB(0xf8f8f8);
    
    [self.view addSubview:_contentView];
}

-(void)createLeftBtn
{
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = LEFTBUTTONFRAME(aHeight);
    [_leftBtn setBackgroundImage:[UIImage imageNamed:@"backBtn_image"] forState:UIControlStateNormal];
    [_leftBtn addTarget:self action:@selector(backPrePage) forControlEvents:UIControlEventTouchUpInside];
    _leftBtn.adjustsImageWhenHighlighted =NO;
    [_navigationBarView addSubview:_leftBtn];
}

- (void)backPrePage
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)changeFrameWhenHiddenNavigationBar
{
    self.navigationBarView.hidden=YES;
    if (IOS7){
        _contentView.frame= CGRectMake(0,aHeight,UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT);
    }else{
        _contentView.frame= CGRectMake(0,0,UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT);
    }

}

-(UIButton *)createButtonOnNavigationWithRect:(CGRect )rect normalImg:(UIImage *)image1  highlightImg:(UIImage *)image2
{
    UIButton * button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(rect.origin.x, rect.origin.y+aHeight, rect.size.width, rect.size.height);
    [button setBackgroundImage:image1 forState:UIControlStateNormal];
    [button setBackgroundImage:image2 forState:UIControlStateHighlighted];
    button.backgroundColor = [UIColor clearColor];
    return button;
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 908120){
        if(buttonIndex == 0){
            DDLOG_CURRENT_METHOD;
            //拨打客服电话
            [XDTools MakeAPhoneCall];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
